package org.zy.commons;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionTool {
	static Reader reader = null;
	static SqlSessionFactory sqlSessionFactory = null;
	static SqlSession session = null;
	static {
		// 1。 创建读取器。读取器=Mybatis资源.得到资源读取器（Mybatis配置文件）
		try {
			reader = Resources.getResourceAsReader("config/Configuration.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

	}
	public static SqlSession openSession() {
		// 2。创建SQL会话工厂。SQL会话工厂=new SQL会话工厂建造器().创建（读取器）
		session = sqlSessionFactory.openSession();
		return session;
	}

	public static void closeSession() {
		session.close();
	}

	public static void commitSession() {
		session.commit();

	}

}
